package com.bigflexlib.x.flash.display.BitmapData
{
	import flash.display.BitmapData;
	import flash.errors.EOFError;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	public class BitmapDataUtil
	{
		
		
		
		
		/**
		 * 
		 * The maximum dimensions of a BitmapData are 2880x2880
		 * 
		 */		
		public static const maxSizeRect:Rectangle = new Rectangle( 0, 0, 2880, 2880 );
		
		
		
		
		
		/**
		 * 
		 * A quick way to get a list of all the unique colors occurring in a bitmap data object
		 * 
		 * 
		 * @param bd
		 * @param colorsAsKeys
		 * @return 
		 * 
		 */		
		public static function getUniqueColors( bd:BitmapData, colorsAsKeys:Boolean = true ):Array
		{
		
			var colors		:Array 		= [];
			var byteArr		:ByteArray 	= bd.getPixels( bd.rect );
			
			byteArr.position = 0;
			
			while ( true )
			{
				try{
					colors[ uint(byteArr.readUnsignedInt( )) ] = true;
				}
				catch ( e:EOFError ){
					break;
				}
			}
			
			if ( colorsAsKeys )
				return colors;
				
			var colors2:Array = [];
			for ( var c:* in colors )
				colors2.push( uint(Number(c)) );
				
			return colors2;
					
		}
		

	}
}